﻿/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

************************************************************************************************/
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.BusinessRules;
using Votations.NSurvey.WebAdmin.UserControls;
using Votations.NSurvey.Emailing;
using Votations.NSurvey.Web;
using Votations.NSurvey.Web.Security;

namespace Votations.NSurvey.WebAdmin
{
    /// <summary>
    /// Build message and send it to the given email	list
    /// </summary>
    public partial class SurveyMailing : PageBase
    {
       
    

        protected void Page_Load(object sender, System.EventArgs e)
        {
            SetupSecurity();
            LocalizePage();

            if (!Page.IsPostBack)
            {
                SurveyList.SurveyId = SurveyId;
                SurveyList.BindDropDown();
            
                FillFields();
            }
        }

        private void SetupSecurity()
        {
            CheckRight(NSurveyRights.AccessMailing, true);
        }

        private void LocalizePage()
        {
            InvitationMailingTitle.Text = GetPageResource("InvitationMailingTitle");
            FromLabel.Text = GetPageResource("FromLabel");
            FromNameLabel.Text = GetPageResource("FromNameLabel");
            SubjectLabel.Text = GetPageResource("SubjectLabel");
            if (!Page.IsPostBack)
            {
                SubjectTextBox.Text = GetPageResource("SubjectTextBox");
                FromNameTextbox.Text = GetPageResource("FromNameTextBox");
            }
            InvitationMessageLabel.Text = GetPageResource("InvitationMessageLabel");
            EmailInvitationLabel.Text = GetPageResource("EmailInvitationLabel");
            AnonymousLabel.Text = GetPageResource("AnonymousLabel");
            SendInvitationButton.Text = GetPageResource("SendInvitationButton");
        }

        /// <summary>
        /// Bind the fields
        /// </summary>
        private void FillFields()
        {
            string surveyLink;
            if (Request.ServerVariables["SERVER_PORT"] != null && Request.ServerVariables["SERVER_PORT"] != "80")
            {
                if (HttpContext.Current.Request.ApplicationPath != "/")
                {
                    surveyLink = string.Format("http://{0}:{1}{2}/survey.aspx?surveyid={3}&uid=[--invitationid-]",
                        Request.ServerVariables["SERVER_NAME"], HttpContext.Current.Request.ApplicationPath, Request.ServerVariables["SERVER_PORT"], SurveyId);
                }
                else
                {
                    surveyLink = string.Format("http://{0}:{1}/survey.aspx?surveyid={2}&uid=[--invitationid-]",
                        Request.ServerVariables["SERVER_NAME"], Request.ServerVariables["SERVER_PORT"], SurveyId);
                }
            }
            else
            {
                if (HttpContext.Current.Request.ApplicationPath != "/")
                {
                    surveyLink = string.Format("http://{0}{1}/survey.aspx?surveyid={2}&uid=[--invitationid-]",
                        Request.ServerVariables["SERVER_NAME"], HttpContext.Current.Request.ApplicationPath, SurveyId);
                }
                else
                {
                    surveyLink = string.Format("http://{0}/survey.aspx?surveyid={1}&uid=[--invitationid-]",
                        Request.ServerVariables["SERVER_NAME"], SurveyId);
                }
            }


            InvitationMessageTextBox.Text =
                string.Format(GetPageResource("InvitationMessageTextBox"), SurveyList.SurveyTitle, surveyLink);
        }


 

        protected void SendInvitationButton_Click(object sender, System.EventArgs e)
        {
            //Init Async call
            ASyncMailing mail = new ASyncMailing();
            ASyncMailingState mailingState = new ASyncMailingState(mail, Session.SessionID);
            AsyncCallback cb = new AsyncCallback(MailingCallback);

            // Build message to send
            EmailingMessage message;
            message = new EmailingMessage();
            message.FromEmail = FromTextBox.Text;
            message.FromName = FromNameTextbox.Text;
            message.Subject = SubjectTextBox.Text;
            message.Body = InvitationMessageTextBox.Text;
            message.Format = EmailFormat.Text;

            // Dummy session variable to init the session in case
            // it was not initialized
            Session["IamADummy"] = "StartIt";

            // Start the mailing
            mail.BeginMailIt(Session.SessionID, SurveyId,
                AnonymousEntriesCheckBox.Checked, MailingListTextBox.Text,
                message, cb, mailingState);

            // Open the poll page while the mailing continues
            UINavigator.NavigateToMailingPoll(SurveyId, MenuIndex);
        }

        public void MailingCallback(IAsyncResult ar)
        {
            // Restore orginial state
            ASyncMailingState mailingState = (ASyncMailingState)ar.AsyncState;

            //retrieve the object on which End needs to be called
            ASyncMailing mailing = mailingState.AsyncMailing;
            mailing.EndMailIt(ar);

            // Let the world know that mailing is finished
            SyncDataStore.SetRecords(mailingState.SessionId, true);
        }
    }
}
